package com.shanzmoo.core.manager.store;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shanzmoo.db.store.entity.StoreMemberStockEntity;
import com.shanzmoo.db.store.entity.StoreMemberStockIoEntity;
import com.shanzmoo.db.store.entity.query.StoreMemberStockIoQuery;
import com.shanzmoo.db.store.entity.query.StoreMemberStockQuery;
import com.shanzmoo.db.store.entity.vo.StoreMemberStockIoVO;
import com.shanzmoo.db.store.entity.vo.StoreMemberStockVO;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.db.store.service.IStoreMemberStockIoService;
import com.shanzmoo.db.store.service.IStoreMemberStockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 仓储用户库存实时明细 管理器
 *
 * @author Gzhao 2021-03-08
 */
@Slf4j
@Component
public class StoreMemberStockManager {

    @Autowired
    private IStoreMemberStockService storeMemberStockService;
    @Autowired
    private IStoreMemberStockIoService storeMemberStockIoService;

    /**
     * 分页查询
     */
    public PageRespBase<StoreMemberStockVO> queryPage(StoreMemberStockQuery query) {
        Page page = PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<StoreMemberStockVO> reList = storeMemberStockService.queryPageList(query);

        PageRespBase<StoreMemberStockVO> result = PageRespBase.create(page, reList);
        PageHelper.clearPage();
        return result;
    }

    /**
     * 库存流水分页查询
     */
    public PageRespBase<StoreMemberStockIoVO> queryIoPage(StoreMemberStockIoQuery query) {
        Page page = PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<StoreMemberStockIoVO> reList = storeMemberStockIoService.queryPageList(query);

        PageRespBase<StoreMemberStockIoVO> result = PageRespBase.create(page, reList);
        PageHelper.clearPage();
        return result;
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.MANDATORY)
    public void updateByIo(StoreMemberStockIoEntity memberStockIoEntity, Integer currentUserId) {
        String sku = memberStockIoEntity.getSku();
        String storeCode = memberStockIoEntity.getStoreCode();
        Integer memberId = memberStockIoEntity.getMemberId();

        Assert.isTrue(StrUtil.isNotEmpty(sku),"未获取到sku信息，更新用户库存失败");
        Assert.isTrue(StrUtil.isNotEmpty(storeCode),"未获取到仓库编码信息，更新用户库存失败");
        Assert.isTrue(ObjectUtil.isNotNull(memberId),"未获取到用户信息，更新用户库存失败");

        StoreMemberStockEntity stockEntity = storeMemberStockService.getBySkuAndStoreCode(memberId, sku, storeCode);
        if (ObjectUtil.isNull(stockEntity)) {
            stockEntity = StoreMemberStockEntity.createInitVersion(memberId, sku, storeCode, currentUserId);
            storeMemberStockService.save(stockEntity);
        }
        storeMemberStockService.updateStockByIo(memberStockIoEntity, stockEntity.getVersionId());
    }
}
